package com.lie.prepare.merge;


import org.junit.Test;
import static com.lie.prepare.util.Print.*;

/**
 * Created by lie on 2018/4/20.
 * 节点
 * @see #testAppend() 这里有个好玩的东西
 * 引用，类似于鬼上身，这个鬼进行的任何控制操作，都会导致那个人进行操作
 *
 * 但是这个鬼上完身，又换去另外一个人上身，除此之外，不做任何操作，那对对象是没有影响的
 */
public class Node {
    public int value;
    public Node nextNode;

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", nextNode=" + nextNode +
                '}';
    }

    private static Node generateLink(int value1, int value2, int value3, int value4, int value5){
        Node node5 = new Node();
        node5.value = value5;
        node5.nextNode = null;

        Node node4 = new Node();
        node4.value = value4;
        node4.nextNode = node5;

        Node node3 = new Node();
        node3.nextNode = node4;
        node3.value = value3;

        Node node2 = new Node();
        node2.nextNode = node3;
        node2.value = value2;

        Node node1 = new Node();
        node1.nextNode = node2;
        node1.value = value1;
        return node1;
    }



    public static Node getLink1(){
        return generateLink(1, 3, 5, 6, 10);
    }
    public static Node getLink2(){
        return generateLink(2, 4, 4, 8, 9);
    }

    @Test
    public void test(){
        Node node1 = generateLink(1, 3, 5, 6, 10);
        Node node2 = generateLink(2, 4, 4, 8, 9);
        print(node1);
        print(node2);
    }

    @Test
    public void testRemove(){
        Node link1 = getLink1();
        print(link1);
        link1 = link1.nextNode;
        print(link1);
    }

    @Test
    public void testAppend(){
        Node l1 = getLink1();
        Node l2 = getLink2();
        append(l1, l2);
    }

    public static Node append(Node l1, Node l2){
        Node temp = l1;
        while (temp.nextNode != null) {
            temp = temp.nextNode;
        }
//        print(temp);
        temp.nextNode = l2;
//        print(l1);
        return l1;
    }
}
